package com.ff.neuropp.controller;


import com.ff.neuropp.domain.Mentor;
import com.ff.neuropp.service.MentorService;
import com.ff.neuropp.util.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/mentor")
@Api(value = "老师管理controller", tags = {"老师管理接口"})
public class PcMentorController {

    @Autowired
    private MentorService mentorService;


    @PostMapping("/create")
    @ApiOperation(value = "添加老师信息",notes = "添加添加老师信息")
    public Map<String,Object> add(@RequestBody Mentor mentor){
        Mentor  data = mentorService.saveMentor(mentor);
        return ResultTool.genSuccessResult(data);
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询信息(PC)", notes = "根据id查询信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "序号") @RequestParam Integer id,
                                        HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Mentor mentor = mentorService.findById(id);
        if (null == mentor) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
        return ResultTool.genSuccessResult("success",mentor);
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改老师信息", notes = "修改老师信息")
    public Map<String,Object> updateCourseCategory(@RequestBody Mentor mentor){
        Mentor cc = mentorService.findById(mentor.getId());
        if(null == cc){ // 没查到该条数据
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"编辑错误,无该条数据！");
        }
        Mentor updateCc = mentorService.saveMentor(mentor);
        return ResultTool.genSuccessResult("修改成功",updateCc);
    }

    @GetMapping("/listpage")
    @ApiOperation(value = "老师信息分页", notes = "老师信息分页")
    public Map<String, Object> findListPage(@Valid Mentor mentor,
                                            HttpServletRequest request) {
        Page<Mentor> list = mentorService.findPage(mentor);
        if(null !=list && list.getTotalElements()>0){
            return ResultTool.genSuccessResult4Page("分页查询成功",list);
        }else{
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }

    }

    @GetMapping("/list")
    @ApiOperation(value = "所有信息", notes = "所有信息")
    public Map<String, Object> findList(@ApiParam(required = false, value = "关键词") @RequestParam(required = false) String name,
                                            HttpServletRequest request) {
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        if (null != name && name.length() > 0) {
            param.put("LIKE_name", name);
        }
        List<Mentor> list = mentorService.findList(param);
        Map<String, Object> result = new HashMap<>();
        if (null != list && list.size() > 0) {
            result.put("total",list.size());
            result.put("list",list);
            return ResultTool.genSuccessResult("success",result);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }

    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除老师信息", notes = "删除老师信息")
    public  Map<String,Object> delete(@ApiParam(required = true,value = "老师id") @RequestParam Integer id){
        Mentor cc = mentorService.findById(id);
        if (null == cc) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "无该条数据");
        }
        mentorService.deleteById(id);
        return ResultTool.genSuccessResult("删除成功");

    }
}
